import socket
import os
import subprocess

# Source ROS环境
ros_setup_path = "/opt/ros/noetic/setup.bash"
mavros_ws_setup_path = "/home/wushuanglang/mavros_ws/devel/setup.bash"
# 动态 source setup.bash
command = ['bash', '-c', f"source {ros_setup_path} && source {mavros_ws_setup_path} && env"]
proc = subprocess.Popen(command, stdout=subprocess.PIPE)
for line in proc.stdout:
    (key, _, value) = line.decode("utf-8").partition("=")
    os.environ[key] = value.strip()

import rospy
from std_msgs.msg import String

def start_client(server_ip='127.0.0.1', server_port=6666):
    # 创建 TCP socket
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    # 连接到服务器
    client_socket.connect((server_ip, server_port))
    
    # 发送数据
    message = "你好，服务器！"
    client_socket.send(message.encode())
    
    # 接收服务器的确认信息
    response = client_socket.recv(1024)
    print(f"收到来自服务器的确认: {response.decode()}")
    
    # 关闭连接
    client_socket.close()

if __name__ == "__main__":
    start_client()

